import { EventDispatcher } from "../../base/event/EventDispatcher";
import { IRecoverable } from "../../base/pool/IRecoverable";

enum Event {
    START = "TASK.EVENT.START",
    PROGRESS = "TASK.EVENT.PROGRESS",
    COMPLETE = "TASK.EVENT.COMPLETE",
    ERROR = "TASK.EVENT.ERROR",
    
}

/**
 * 任务处理
 */
export class Task extends EventDispatcher implements IRecoverable {
    static Event = Event;
    protected _percent:number = 0;
    /**
     * 开始
     * @param data 
     */
    start(...data: any): void {

    }

    /** 规范事件抛出格式，供内外部调用 */
    onStart():void {
        this.event(Event.START, [Event.START, this]);
    }

    onComplete():void {
        this.event(Event.COMPLETE, [Event.COMPLETE, this]);
    }

    onError(code:string, msg:string):void {
        this.event(Event.ERROR, [Event.ERROR, this, {code:code, msg:msg}]);
    }

    onProgress(percent:number):void {
        console.assert(percent >= 0 && percent <= 1, "百分比只能在0-1之间", percent);
        this._percent = percent;
        this.event(Event.PROGRESS, [Event.PROGRESS, this, percent]);
    }
    /** 规范事件抛出格式，供内外部调用 */

    get percent():number{
        return this._percent;
    }

    
    reset(): void {
        this.offAllEvent();
    }

    /**
     * 销毁
     */
    destroy(): void {
        this.offAllEvent();
    }
}
